package com.shr.exam.zte;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main1 {
	public int getMaxGroup(int numGroups, int numMics, int[] groupList){
		int preMics = numMics - numGroups ;
		List<Integer> newGroupList = new ArrayList<>();
		for(int i = 0;i<groupList.length;i++)
			newGroupList.add(groupList[i]);
		while(preMics > 0){
			int maxIdx = getMaxIndex(newGroupList) ;
			int maxVal = newGroupList.get(maxIdx); 
			int reduceVal = (maxVal + 1) / 2 ;
			newGroupList.set(maxIdx, reduceVal);
			newGroupList.add(maxVal - reduceVal) ;
			preMics -- ;
		}
		return Collections.max(newGroupList) ;
	}

	private int getMaxIndex(List<Integer> groupList) {
		int maxIdx = 0;
		int max = Integer.MIN_VALUE ;
		for(int i = 0;i<groupList.size();i++){
			if(groupList.get(i) > max){
				max = groupList.get(i);
				maxIdx = i;
			}
		}
		return maxIdx;
	}
}
